import { SlateRange } from "@wangeditor/editor";
import { message } from "ant-design-vue";
import BaseFn from "../base-fn";
// import { emitter } from '@/event-bus';

class PinyinFn extends BaseFn {
    constructor(editor) {
        super(editor);
    }

    getValue() {
        return super.getValue();
    }

    isDisabled() {
        if (super.isDisabled()) return true;
        const { selection } = this.editor;
        if (!selection) return true;
        if (SlateRange.isCollapsed(selection)) {
            message.warn("请框选一个中文字符")
            //   emitter.emit('warn', '请框选一个中文字符');
            return true;
        }

        const value = this.getValue();
        if (value.length != 1) {
            message.warn("请框选一个中文字符")
            //   emitter.emit('warn', '请框选一个中文字符');
            return true;
        }

        if (!/^[\u4E00-\u9FA5]$/gi.test(value)) {
            message.warn("请框选一个中文字符")
            //   emitter.emit('warn', '请框选一个中文字符');
            return true;
        }

        return false;
    }

    exec(opt) {
        this.editor.restoreSelection();
        if (this.isDisabled()) return;
        const value = this.getValue();
        if (value == null) return;

        const node = {
            type: "ssml-phoneme",
            alphabet: "py",
            ph: opt.value,
            remark: opt.label,
            children: [{ text: value }],
        };

        this.editor.insertNode(node);
    }
}

export default PinyinFn;
